package com.example.mvp.mvppromote.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.mvp.mvppromote.R;
import com.example.mvp.mvppromote.base.BaseFragment;
import com.example.mvp.mvppromote.model.BandItemBean;
import com.example.mvp.mvppromote.presenter.BandPresenter;
import com.example.mvp.mvppromote.ui.adapter.BandAdapter;
import com.example.mvp.mvppromote.ui.iview.IBandView;
import com.example.mvp.mvppromote.utils.ToaskUtil;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by wty on 2016/6/24 0024.
 */
public class BandFragment extends BaseFragment<BandPresenter> implements IBandView, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.progressBar)
    android.widget.ProgressBar progressBar;
    @BindView(R.id.swipe_target)
    android.support.v7.widget.RecyclerView swipeTarget;
    @BindView(R.id.swipeToLoadLayout)
    android.support.v4.widget.SwipeRefreshLayout swipeToLoadLayout;
    private BandAdapter bandAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private int pastVisiblesItems, visibleItemCount, totalItemCount;
    private int currentOffset;
    private ArrayList<BandItemBean> bandItemBeen = new ArrayList<BandItemBean>();
    private Context context;
    private boolean visi;

    @Override
    public void updateList(ArrayList<BandItemBean> bandItemBeens) {
        currentOffset++;
        this.bandItemBeen.addAll(bandItemBeens);
        bandAdapter.setVisi(visi);
        bandAdapter.notifyDataSetChanged();
    }

    private void initView() {
        swipeToLoadLayout.setOnRefreshListener(this);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        swipeTarget.setLayoutManager(mLinearLayoutManager);
        swipeToLoadLayout.setColorSchemeColors(Color.BLUE, Color.GREEN, Color.RED, Color.YELLOW);
        swipeTarget.setHasFixedSize(true);
        // swipeTarget.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        swipeTarget.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    visibleItemCount = mLinearLayoutManager.getChildCount();
                    totalItemCount = mLinearLayoutManager.getItemCount();
                    pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();

                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        onLoadMore();
                    }
                }
            }
        });
        bandAdapter = new BandAdapter(bandItemBeen, context);
        swipeTarget.setAdapter(bandAdapter);
        onRefresh();
    }


    public void onLoadMore() {
        visi = true;
        mPresenter.getBand(currentOffset);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected BandPresenter getPresenter() {
        return new BandPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_band;
    }

    @Override
    protected void initData() {
        context = getActivity();
        butterknife.ButterKnife.bind(this, view);
        initView();
    }

    @Override
    public void onRefresh() {
        visi = false;
        currentOffset = 0;
        bandItemBeen.clear();
        bandAdapter.notifyDataSetChanged();
        mPresenter.getBand(currentOffset);
    }

    @Override
    public Context getMyContext() {
        return context;
    }
}
